package com.bluedon.ism.commons.util;

import com.bluedon.ism.commons.SystemContext;
import com.bluedon.ism.commons.dao.AccountDAO;
import com.bluedon.ism.commons.dao.OrganizationDAO;
import com.bluedon.ism.commons.entity.Account;
import com.bluedon.ism.commons.entity.Organization;
import org.springframework.context.ApplicationContext;

/**
 * jsp页面根据account表主键aid加载对象，避免每次在service层都需要专门对account表进行查询
 *
 */
public class GetEntityInJsp {

    
    /**
     * 迭代struts的iterator标签时候，把aid设在页面的page scope里面<br/>
     * &lts:iterator value="pager.list" status="rowstatus" var="acc"&gt <br/>
     * &lts:set scope="page" name="accountId" value="#acc.aid"/&gt <br/> 
     * <code>                  
         Account account = GetAccount.getAccountByAid(Long.valueOf(pageContext.getAttribute("accountId").toString()));
         pageContext.setAttribute("nacc", account);
       </code>
     * @param aid account表主键id
     * @return account对象
     */
    public static Account getAccountByAid(Long aid) {
        ApplicationContext context = (ApplicationContext) SystemContext
                .getContext().get(SystemContext.SPRING_APPLICATION_CONTEXT);
        AccountDAO accountDAO = (AccountDAO)context.getBean("accountDAO");
        return accountDAO.getAccountById(aid);
    }
    

    public static Organization getOrgByOid(String oid) {
        ApplicationContext context = (ApplicationContext) SystemContext
                .getContext().get(SystemContext.SPRING_APPLICATION_CONTEXT);
        OrganizationDAO organizationDAO = context.getBean("organizationDAO", OrganizationDAO.class);
        return organizationDAO.getOrgById(oid);
    }
}
